<template>
  <div class="login_page">
    <van-nav-bar
      title="账号登录"
      left-arrow
      @click-left="onClickLeft"
      class="header_change_color"
    />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写账号' },
          { pattern: /^[a-zA-Z0-9_-]{4,16}$/, message: '输入的格式不对' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^[0-9]{6}$/, message: '密码格式不对' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>

    <div class="am-flexbox-item">还没有账号，去注册</div>
  </div>
</template>

<script>
import { userLoginAPI } from "@/api";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    async onSubmit() {
      try {
        const res = await userLoginAPI(this.username, this.password);
        console.log(888, res.data.body.token);
        this.$store.commit("SET_TOKEN", res.data.body.token);
        this.$router.push({ path: "/layout/my" });
        this.$toast("登录成功");
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
.login_page {
  height: 100vh;
  background-color: #fff;
}
.login_page .header_change_color.van-nav-bar {
  background-color: #21b97a;
}
.login_page .header_change_color.van-nav-bar .van-icon {
  color: #fff;
}
.login_page .header_change_color.van-nav-bar .van-nav-bar__title {
  color: #fff;
}
.login_page .van-button--info {
  background-color: #21b97a;
  border-radius: 0;
}
.login_page .am-flexbox-item {
  text-align: center;
}
.login_page .am-flexbox-item {
  font-size: 0.37rem;
}
</style>
